package com.jason.randomgame;

import min3d.Shared;
import min3d.Utils;
import min3d.core.RendererActivity;
import min3d.objectPrimitives.HardBox;
import min3d.objectPrimitives.HardBox.Face;
import min3d.objectPrimitives.Rectangle;
import min3d.vos.Light;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;

import com.flurry.android.FlurryAgent;

@SuppressLint("ParserError")
public class GameDice3D extends RendererActivity  implements View.OnClickListener{
	HardBox _cube;
	Rectangle rectA;
	Rectangle rectB;
	Rectangle rectC;
	Rectangle rectD;
	Rectangle rectE;
	Rectangle rectF;
	private LinearLayout customCavas;
	private boolean startRoll = false;
	private final static int DEFAULT_A = 200;
	int a = DEFAULT_A;
	Handler mHandler = new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.dice);
        customCavas = (LinearLayout) findViewById(R.id.other);
        customCavas.addView(_glSurfaceView);
	}
	

    @Override 
	protected void glSurfaceViewConfig()
    {
		// !important
		_glSurfaceView.setZOrderOnTop(true);
        _glSurfaceView.setEGLConfigChooser(8,8,8,8, 16, 0);
        _glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    }
	
	@Override
	public void initScene() {
		super.initScene();

		// !important
		scene.backgroundColor().setAll(0x00000000);
		scene.lights().add(new Light());
		_cube = new HardBox(1, 6);
		scene.addChild(_cube);

		// Create a Bitmap. Here we're generating it from an embedded resource,
		// but the Bitmap could be created in any manner (eg, dynamically).

		Bitmap a = Utils.makeBitmapFromResourceId(this, R.drawable.dice_one);
		Bitmap b = Utils.makeBitmapFromResourceId(this, R.drawable.dice_two);
		Bitmap c = Utils.makeBitmapFromResourceId(this, R.drawable.dice_three);
		Bitmap d = Utils.makeBitmapFromResourceId(this, R.drawable.dice_four);
		Bitmap e = Utils.makeBitmapFromResourceId(this, R.drawable.dice_five);
		Bitmap f = Utils.makeBitmapFromResourceId(this, R.drawable.dice_six);
		
		// Upload the Bitmap via TextureManager and assign it a 
		// textureId ("uglysquares").
		
		Shared.textureManager().addTextureId(a, "a", true);
		Shared.textureManager().addTextureId(b, "b", true);
		Shared.textureManager().addTextureId(c, "c", true);
		Shared.textureManager().addTextureId(d, "d", true);
		Shared.textureManager().addTextureId(e, "e", true);
		Shared.textureManager().addTextureId(f, "f", true);
		
		// Add it to the TexturesList held by the Object3d, 
		// and it will be duly rendered.
		_cube.addTexture(Face.Down, a, "a");
		_cube.addTexture(Face.East, b, "b");
		_cube.addTexture(Face.West, d, "d");
		_cube.addTexture(Face.North, c, "c");
		_cube.addTexture(Face.South, e, "e");
		_cube.addTexture(Face.Up, f, "f");
		// Unless you have a specific reason for doing so, recycle the Bitmap,
		// as it is no longer necessary.
		
		a.recycle();
		b.recycle();
		c.recycle();
		d.recycle();
		e.recycle();
		f.recycle();
		
		_cube.rotation().y = (float) (Math.random() * 90.0f);
		_cube.rotation().z = (float) (Math.random() * 90.0f);
	}
	@Override
	public void updateScene() {
		super.updateScene();
		if(! startRoll){
			_cube.rotation().y += 0.1 * (a = a - 10 > 0 ? a - 10 : 0);
			_cube.rotation().z += 0.01f * (a);
		} else {
			a = DEFAULT_A;
			_cube.rotation().y += 0.2 * a;
			_cube.rotation().z += 0.01f * a;
		}
	}
	@Override
	public void onClick(View v) {
    	FlurryAgent.logEvent("dice 3D game");
		startRoll = true;
		mHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				startRoll = false;
			}
		}, 1000);
	}
    
}
